package com.uhylab.uhuvinetest.activity;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.StringRes;
import android.support.design.widget.NavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.uhylab.uhuvinetest.MyService;
import com.uhylab.uhuvinetest.R;
import com.uhylab.uhuvinetest.base.UHBaseFragment;
import com.uhylab.uhuvinetest.base.UHMainBaseActivity;
import com.uhylab.uhuvinetest.fragment.UHTest;
import com.uhylab.uhuvinetest.fragment.UHUserCenterFragment;
import com.uhylab.uhuvinetest.utils.sharedpreutils.SharedPreUtils;
import com.uhylab.uhuvinetest.view.NoScrollViewPager;
import com.zhy.autolayout.AutoRelativeLayout;

import java.util.ArrayList;
import java.util.List;


//public class UHMainActivity extends UHBaseActivity implements NavigationView.OnNavigationItemSelectedListener, UHHomeFragment.GoDirect, UHHistoryFragment.GoPhoto, UHHomeFragment.GoPhoto, UHUserCenterFragment.setTextView, View.OnClickListener {
public class UHMainActivity2 extends UHMainBaseActivity implements UHUserCenterFragment.setTextView, View.OnClickListener {


    private List<UHBaseFragment> fragments;
    private AutoRelativeLayout mToolbar;
    private DrawerLayout mDrawerLayout;
    private MyActionBarDrawerToggle myActionBarDrawerToggle;
    private NavigationView navigationView;
    private TextView textView;
    private long exitTime = 0;// 双击返回间隔时间
    //    private ImageButton button_next;
    private TextView childAt;
    private ImageView main_more, img_instructions;
    private boolean first;
    private Intent mStartIntent;
    private boolean is_start_service = false;
    private RadioGroup tg_bottom_tag;
    private NoScrollViewPager viewpager_content;


    @Override
    public int bindLayout() {
        Log.e("TAG", "bindLayout");
        return R.layout.activity_main;
    }

    @Override
    public void initView(View view) {
        mStartIntent = new Intent(this, MyService.class);
        startService(mStartIntent);
        is_start_service = true;
        // mToolbar = (AutoRelativeLayout) view.findViewById(R.id.toolbar);
        //   mDrawerLayout = (DrawerLayout) view.findViewById(R.id.drawer_layout);
        //设置导航栏NavigationView的点击事件
        //   navigationView = (NavigationView) view.findViewById(R.id.navigation_view);
//        button_next = (ImageButton) findViewById(R.id.button_next);
        //  main_more = (ImageView) findViewById(R.id.main_more);

        tg_bottom_tag = (RadioGroup) findViewById(R.id.tg_bottom_tag);
        tg_bottom_tag.check(R.id.rb_home);
        viewpager_content = (NoScrollViewPager) findViewById(R.id.viewpager_content);
    }


    @Override
    public void doBusiness(Context mContext) {
        // 初始化数据
        initData();
        // 初始化监听
        initViewListener();
    }

    private void initViewListener() {
//        mDrawerLayout.setScrimColor(getResources().getColor(R.color.transparent));
//        mDrawerLayout.setDrawerListener(myActionBarDrawerToggle);
//        navigationView.setNavigationItemSelectedListener(this);
//        commitFragment(fragments.get(0), R.string.tool_home);
        first = SharedPreUtils.getBoolean(getContext(), "is_first_open_activity", false);
        if (!first) {
            img_instructions.setVisibility(View.VISIBLE);
        }
        if (SharedPreUtils.getBoolean(getContext(), "is_Login", false)) {
            //  navigationView.getMenu().getItem(3).setTitle("注销");
        }
        img_instructions.setOnClickListener(this);
//        main_more.setOnClickListener(this);

        viewpager_content.setAdapter(new ContentFragmentAdapter(getSupportFragmentManager()));

        //监听RadioGroup的状态
        tg_bottom_tag.setOnCheckedChangeListener(new MyOnCheckedChangeListener());

        Log.e("TAG", "监听RadioGroup的状态");

        //设置监听页面的改变
        viewpager_content.setOnPageChangeListener(new MyOnPageChangeListener());

        Log.e("TAG", "设置监听页面的改变");

        viewpager_content.setCurrentItem(0, false);

    }

    class MyOnPageChangeListener implements ViewPager.OnPageChangeListener {

        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {
            //basePager.initData();
            fragments.get(position).initData();
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    }

    class MyOnCheckedChangeListener implements RadioGroup.OnCheckedChangeListener {

        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            switch (checkedId) {
                case R.id.rb_home:
                    viewpager_content.setCurrentItem(0, false);
//                    commitFragment(fragments.get(0), R.string.tool_home);

                    break;
                case R.id.rb_history:
                    viewpager_content.setCurrentItem(1, false);
//                    commitFragment(fragments.get(1), R.string.tool_usercenter);
                    break;
                case R.id.rb_shop:
                    viewpager_content.setCurrentItem(2, false);
//                    commitFragment(fragments.get(2), R.string.tool_history);
                    break;
                case R.id.rb_user:
                    viewpager_content.setCurrentItem(3, false);
                    break;
            }
        }
    }

    private void initData() {
//        View headerView = navigationView.getHeaderView(0);
//        textView = (TextView) headerView.findViewById(R.id.id_link);
        fragments = new ArrayList<>();
//        UHHomeFragment uhHomeFragment = new UHHomeFragment();
//
//        UHHistoryFragment uhHistoryFragment = new UHHistoryFragment();
//        UHUserCenterFragment uhUserCenterFragment = new UHUserCenterFragment();
//
//        fragments.add(new UHHomeFragment());
//
//        fragments.add(new UHHistoryFragment());
//        fragments.add(new UHUserCenterFragment());
//        fragments.add(new UHUserCenterFragment());

        fragments.add(new UHTest());
        fragments.add(new UHTest());
        fragments.add(new UHTest());
        fragments.add(new UHTest());
        Log.e("TAG", fragments.size() + "fragments");

//        fragments.add(new UHHomePager(this));
//        fragments.add(new UHHistoryPager(this));
//        fragments.add(new UHShopPager(this));
//        fragments.add(new UHUserPager(this));

//        childAt = (TextView) mToolbar.getChildAt(1);
//        childAt.setText(R.string.tool_home);
//        // 设置返回键监听
//        myActionBarDrawerToggle = new MyActionBarDrawerToggle(this, mDrawerLayout,
//                R.string.drawer_open, R.string.drawer_close);
//        myActionBarDrawerToggle.syncState();//初始化状态

    }

//    @Override
//    public boolean onNavigationItemSelected(MenuItem item) {
//
//        switch (item.getItemId()) {
//            case R.id.item_home:
//                commitFragment(fragments.get(0), R.string.tool_home);
////                button_next.setVisibility(View.GONE);
//                break;
//            case R.id.item_usercenter:
//                commitFragment(fragments.get(1), R.string.tool_usercenter);
////                button_next.setVisibility(View.GONE);
//                break;
//
//            case R.id.item_history:
//                commitFragment(fragments.get(2), R.string.tool_history);
////                button_next.setVisibility(View.GONE);
//                break;
//            case R.id.item_direct:
//
//                if (!SharedPreUtils.getBoolean(getContext(), "is_Login", false)) {
//
//                    Intent intent = new Intent(this, UHLogonActivity.class);
//                    startActivity(intent);
//
//                } else {
//                    SharedPreUtils.setBoolean(getContext(), "is_Login", false);
//                    navigationView.getMenu().getItem(3).setTitle("登陆");
//                    finish();
//                    Intent intent = new Intent(this, UHMainActivity.class);
//                    startActivity(intent);
//
//
//                }
////                button_next.setVisibility(View.GONE);
//                break;
//
//        }
////        item.setChecked(true);//点击了把它设为选中状态
//        mDrawerLayout.closeDrawers();//关闭抽屉
//        return true;
//    }

    public void commitFragment(Fragment fragment, @StringRes int resId) {
        getSupportFragmentManager().beginTransaction().replace(R.id.viewpager_content, fragment).commit();
        childAt.setText(resId);
    }

    @Override
    public void resume() {
        if (SharedPreUtils.getBoolean(getContext(), "is_Login", false)) {
            //   navigationView.getMenu().getItem(3).setTitle("注销");
        } else {
            //   navigationView.getMenu().getItem(3).setTitle("登陆");
        }
    }

    @Override
    public void destroy() {
        if (is_start_service) {
            stopService(mStartIntent);
            is_start_service = false;
        }

    }


//    @Override
//    public void gotoPhoto() {
//        commitFragment(fragments.get(2), R.string.tool_photo);
//    }
//
//    @Override
//    public void gotoDirect() {
//        img_instructions.setImageResource(R.drawable.direct1);
//        img_instructions.setVisibility(View.VISIBLE);
//        first = false;
//    }


    /**
     * 双击返回的Toast提示
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
//            mDrawerLayout.closeDrawers();//关闭抽屉
            if ((System.currentTimeMillis() - exitTime) > 2000) {
                Toast.makeText(UHMainActivity2.this, "再按一次退出", Toast.LENGTH_SHORT).show();
                exitTime = System.currentTimeMillis();
            } else {
                finish();
            }

        }
        return false;
    }


    @Override
    public void set(String s) {
        textView.setText(s);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
//            case R.id.main_more:
//                if (mDrawerLayout.isDrawerOpen(navigationView)) {
//                    mDrawerLayout.closeDrawers();//关闭抽屉
//
//                } else {
//                    mDrawerLayout.openDrawer(navigationView);
//
//                }
//                break;

//            case R.id.img_instructions:
//                if (!first) {
//                    img_instructions.setImageResource(R.drawable.direct2);
//                    first = true;
//                } else {
//                    img_instructions.setVisibility(View.GONE);
//                    SharedPreUtils.setBoolean(getContext(), "is_first_open_activity", true);
//                }
////                showShare();
//                break;
        }
    }



    class ContentFragmentAdapter extends FragmentPagerAdapter {
        public ContentFragmentAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return fragments.get(position);
        }

        @Override
        public int getCount() {
            return fragments.size();
        }

//        @Override
//        public int getCount() {
//            return fragments.size();
//        }
//
//        @Override
//        public boolean isViewFromObject(View view, Object object) {
//            return view == object;
//        }
//
//        @Override
//        public Object instantiateItem(ViewGroup container, int position) {
//            UHBasePager basePager = fragments.get(position);
//            View rootView = basePager.rootView;
//            container.addView(rootView);
//
//            //basePager.initData();
//
//            return rootView;
//        }
//
//
//
//        @Override
//        public void destroyItem(ViewGroup container, int position, Object object) {
//            container.removeView((View) object);
//        }
//    }
    }

}
